package com.genersoft.iot.vmp.vmanager.record;


import com.genersoft.iot.vmp.gb28181.transmit.request.impl.InviteRequestProcessor;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.*;
import java.util.Arrays;


public class AudioStream extends Thread {



    public static void send() throws IOException {

        long ssrc = 255;
        int send_interval_ms = 50;
        int audio_need_len = 400;
//        int localPort = 56200;
//        int peerPort = 15062;
        String peerIP = "175.10.15.231";
        int localPort = 8000;
        String port0= InviteRequestProcessor.port1;
        Integer port=Integer.valueOf(port0);
        int peerPort = port;
        DatagramSocket ds = null;
        InetAddress peerAddress = null;
        InputStream audio_stream = new FileInputStream("E:\\szrm\\workspace\\study\\simpProject\\src\\com\\mq.g711a");

        try {
            ds = new DatagramSocket(localPort);
        } catch (SocketException e) {
            e.printStackTrace();
            System.exit(1);
        }

        try {
            peerAddress = InetAddress.getByName(peerIP);
        } catch (UnknownHostException e) {
            e.printStackTrace();
            System.exit(1);
        }

        RtpPacket rtp = new RtpPacket(ssrc);
        byte[] audio_data = new byte[audio_need_len];
        int i=0;
        while (audio_stream.read(audio_data) == audio_need_len) {

            byte[] payload = rtp.packet_g711a(audio_data, audio_data.length);
            DatagramPacket dp = new DatagramPacket(payload, payload.length, peerAddress, peerPort);

            ds.send(dp);
            System.out.println(Arrays.toString(payload));
            System.out.println("发送"+i+"消息-----------------");
            i++;
            try {
                Thread.sleep(send_interval_ms);

            } catch (InterruptedException e) {
                e.printStackTrace();
                break;
            }
        }
        ds.close();
    }
}
